﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WeifenLuo.WinFormsUI.Docking;
using UnrealDebugger.Panels;
using System.Windows.Forms;
using System.IO;

namespace UnrealDebugger
{
    // Panels

    partial class UnrealDebuggerIDE
    {
        private DeserializeDockContent _deserializeDockContent = null;
        private OutputPanel _outputPanel = null;
        private OutlinePanel _outlinePanel = null;
        private ExplorerPanel _explorerPanel = null;
        private CallstackPanel _callstackPanel = null;
        private LocalWatchPanel _localWatchPanel = null;
        private GlobalWatchPanel _globalWatchPanel = null;
        private UserWatchPanel _userWatchPanel = null;
        private ConfigurationPanel _configurationPanel = null;
        private BreakpointsPanel _breakpointsPanel = null;
        private FileSearchPanel _fileSearchPanel = null;
        private FindPanel _findPanel = null;
        private CommandPanel _commandPanel = null;

        private void CreatePanels()
        {
            _outputPanel = new OutputPanel();
            _outlinePanel = new OutlinePanel();
            _explorerPanel = new ExplorerPanel();
            _callstackPanel = new CallstackPanel();
            _localWatchPanel = new LocalWatchPanel();
            _globalWatchPanel = new GlobalWatchPanel();
            _userWatchPanel = new UserWatchPanel();
            _configurationPanel = new ConfigurationPanel();
            _breakpointsPanel = new BreakpointsPanel();
            _fileSearchPanel = new FileSearchPanel();
            _findPanel = new FindPanel();
            _commandPanel = new CommandPanel();
        }

        private IDockContent GetContentFromPersistString(string persistString)
        {
            if (persistString == typeof(OutputPanel).ToString()) return _outputPanel;
            else if (persistString == typeof(OutlinePanel).ToString()) return _outlinePanel;
            else if (persistString == typeof(ExplorerPanel).ToString()) return _explorerPanel;
            else if (persistString == typeof(CallstackPanel).ToString()) return _callstackPanel;
            else if (persistString == typeof(LocalWatchPanel).ToString()) return _localWatchPanel;
            else if (persistString == typeof(GlobalWatchPanel).ToString()) return _globalWatchPanel;
            else if (persistString == typeof(UserWatchPanel).ToString()) return _userWatchPanel;
            else if (persistString == typeof(ConfigurationPanel).ToString()) return _configurationPanel;
            else if (persistString == typeof(BreakpointsPanel).ToString()) return _breakpointsPanel;
            else if (persistString == typeof(FileSearchPanel).ToString()) return _fileSearchPanel;
            else if (persistString == typeof(FindPanel).ToString()) return _findPanel;
            else if (persistString == typeof(CommandPanel).ToString()) return _commandPanel;
            else
            {
                string[] parsedStrings = persistString.Split(new char[] { ',' });
                if (parsedStrings.Length != 2) { return null; }
                if (parsedStrings[0] != typeof(Document).ToString()) { return null; }
                if (parsedStrings[1] == string.Empty || !File.Exists(parsedStrings[1])) { return null; }

                Document document = new Document();
                document.FileName = parsedStrings[1];

                return document;
            }
        }

        private void RegisterPanel(UnrealDebugger.Panels.Panel panel, string menuItemText)
        {
            ToolStripMenuItem item = new System.Windows.Forms.ToolStripMenuItem();
            this.viewToolStripMenuItem.DropDownItems.Add(item);

            item.Name = panel.GetType().ToString();
            item.Text = menuItemText;
            item.Image = panel.Icon.ToBitmap();
            item.Tag = panel;
            item.Checked = panel.Visible;
            item.Click += new System.EventHandler(this.panelToolStripMenuItem_Click);
        }

        private void viewToolStripMenuItem_DropDownOpening(object sender, EventArgs e)
        {
            foreach (ToolStripMenuItem item in this.viewToolStripMenuItem.DropDownItems)
            {
                UnrealDebugger.Panels.Panel panel = item.Tag as UnrealDebugger.Panels.Panel;
                item.Checked = panel != null && panel.Visible;
            }
        }

        private void panelToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ToolStripMenuItem item = sender as ToolStripMenuItem;
            UnrealDebugger.Panels.Panel panel = item != null ? item.Tag as UnrealDebugger.Panels.Panel : null;

            if (panel != null)
            {
                if (panel.Visible)
                {
                    panel.Hide();
                    item.Checked = false;
                }
                else
                {
                    panel.Show(this.dockPanelDebugger);
                    item.Checked = true;
                }
            }
        }
    }
}
